package org.egedede.commons.apps;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class TangoGallery extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2989824763420518402L;

	public TangoGallery() {
		super("Tang Gallery");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(200, 400);
		String parent = "org/freedesktop/tango/16x16/";
		ListModel listModel = this.getListModel(parent);
		final JPanel contentPane = new JPanel(new BorderLayout());
		final JList list = new JList(listModel);
		list.addListSelectionListener(new ListSelectionListener() {

			public void valueChanged(ListSelectionEvent e) {
				JarEntry entry = (JarEntry) list.getSelectedValue();
				List<ImageIcon> icons = TangoGallery.this.getIcons(entry
						.getName());
				JPanel panel = new JPanel(new GridLayout(4, 0, 10, 10));
				for (ImageIcon icon : icons) {
					JLabel label = new JLabel(icon);
					label.setToolTipText(icon.getDescription());
					panel.add(label);
				}
				contentPane.add(panel);
				contentPane.revalidate();
			}

		});
		contentPane.add(new JScrollPane(list), BorderLayout.WEST);
		this.setContentPane(contentPane);
	}

	private ListModel getListModel(String parent) {
		DefaultListModel result = new DefaultListModel();
		URL resource = TangoGallery.class.getClassLoader().getResource(parent);

		String filepath = resource.getFile();
		JarFile jarFile;
		try {
			jarFile = new JarFile(new File(filepath.substring(5,
					filepath.indexOf("!"))));
		} catch (IOException e) {
			e.printStackTrace();
			return result;
		}
		Enumeration<JarEntry> entries = jarFile.entries();
		while (entries.hasMoreElements()) {
			JarEntry entry = entries.nextElement();
			if (entry.getName().startsWith(parent)
					&& !entry.getName().equals(parent) && entry.isDirectory())
				result.addElement(entry);
		}
		return result;
	}

	private List<ImageIcon> getIcons(String parent) {
		List<ImageIcon> result = new LinkedList<ImageIcon>();
		URL resource = TangoGallery.class.getClassLoader().getResource(parent);

		String filepath = resource.getFile();
		JarFile jarFile;
		try {
			jarFile = new JarFile(new File(filepath.substring(5,
					filepath.indexOf("!"))));
		} catch (IOException e) {
			e.printStackTrace();
			return result;
		}
		Enumeration<JarEntry> entries = jarFile.entries();
		while (entries.hasMoreElements()) {
			JarEntry entry = entries.nextElement();
			if (entry.getName().startsWith(parent)
					&& !entry.getName().equals(parent)) {
				byte[] bytes = new byte[1];
				try {
					bytes = this.getBytes(jarFile.getInputStream(entry),
							entry.getSize());
				} catch (IOException e) {
					e.printStackTrace();
				}
				result.add(new ImageIcon(bytes, entry.getName().substring(
						entry.getName().lastIndexOf("/"))));
			}
		}
		return result;
	}

	private byte[] getBytes(InputStream inputStream, long size) {
		byte[] result = new byte[(int) size];
		try {
			inputStream.read(result);
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static void main(String[] args) {
		new TangoGallery().setVisible(true);
	}
}
